package org.tea.inventory.admin.web.admin;

import com.github.pagehelper.PageInfo;

import org.tea.inventory.base.util.ResponseUtil;
import org.tea.inventory.db.domain.AlumniUser;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.tea.inventory.admin.annotation.LoginAdmin;
import org.tea.inventory.db.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/user")
public class UserController {
	private final Log logger = LogFactory.getLog(UserController.class);

	@Autowired
	private UserService userService;

	/**
	 * 查询非审核中的用户列表
	 * @param adminId
	 * @param alumniUser
	 * @param page
	 * @param limit
	 * @param sort
	 * @param order
	 * @return
	 */
	@GetMapping("/list")
	public Object list(@LoginAdmin Integer adminId,
			AlumniUser alumniUser,
			@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "limit", defaultValue = "10") Integer limit,
			String sort, String order){
		if(adminId == null){
			return ResponseUtil.fail401();
		}

		PageInfo<AlumniUser> pageInfo = userService.queryAlumniUser(alumniUser, page, limit, sort, order);
		Map<String, Object> data = new HashMap<>();
		data.put("total", pageInfo.getTotal());
		data.put("items", pageInfo.getList());

		return ResponseUtil.ok(data);
	}

	/**
	 * 更新user信息
	 */
	@PostMapping("update")
	public Object update(@LoginAdmin Integer adminId, @RequestBody AlumniUser user) {

		if(adminId == null) {
			return ResponseUtil.unlogin();
		}
		if(user == null)
			return  ResponseUtil.fail402();

		userService.updateUser(user);

		return ResponseUtil.ok(user);
	}

	/**
	 * 筛选导出全部用户
	 * @param adminId
	 * @param alumniUser
	 * @return
	 */
	@GetMapping("/exportAllUser")
	public Object exportAllUser(@LoginAdmin Integer adminId,
					   AlumniUser alumniUser){
		if(adminId == null){
			return ResponseUtil.fail401();
		}

		List<AlumniUser> pageInfo = userService.allAlumniUser(alumniUser);
		if(pageInfo.size() >= 5000){
			ResponseUtil.fail("数据大于 5000 条Excel将无法打开，请过滤查询条件");
		}
		Map<String, Object> data = new HashMap<>();
		data.put("items", pageInfo);

		return ResponseUtil.ok(data);
	}

	/**
	 *查询游客角色的用户列表
	 * @param adminId
	 * @param page
	 * @param limit
	 * @param sort
	 * @param order
	 * @return
	 */
	@GetMapping("/findYKList")
	public Object findYKList(@LoginAdmin Integer adminId,
							 @RequestParam(value = "page", defaultValue = "1") Integer page,
							 @RequestParam(value = "limit", defaultValue = "10") Integer limit,
							 String sort, String order){
		if(adminId == null){
			return ResponseUtil.fail401();
		}
		PageInfo<AlumniUser> pageInfo = userService.queryAlumniUserNotYK(page, limit, sort, order);
		Map<String, Object> data = new HashMap<>();
		data.put("total", pageInfo.getTotal());
		data.put("items", pageInfo.getList());
		return ResponseUtil.ok(data);
	}

	/**
	 * 查询待审核的用户列表
	 * @param adminId
	 * @param page
	 * @param limit
	 * @param sort
	 * @param order
	 * @return
	 */
	@RequestMapping(value = "searchAwaitAuditUserList")
	public Object searchAwaitAuditUserList(@LoginAdmin Integer adminId,
			String userName, String phone, String temRoleType,
			@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "limit", defaultValue = "10") Integer limit,
			String sort, String order) {
		if(adminId == null){
			return ResponseUtil.fail401();
		}
		AlumniUser example = new AlumniUser();

        if(!StringUtils.isEmpty(userName)){
            example.setUserName(userName);
        }
        if(!StringUtils.isEmpty(phone)){
			example.setPhone(phone);
		}
		if(!StringUtils.isEmpty(temRoleType)){
			example.setTemRoleType(temRoleType);
		}
        example.setAuditStatus(1);
		PageInfo<AlumniUser> pageInfo = userService.querySelective(example, page, limit, sort, order);
		Map<String, Object> data = new HashMap<>();
		data.put("total", pageInfo.getTotal());
		data.put("items", pageInfo.getList());
		return ResponseUtil.ok(data);
	}

	/**
	 * 批量认证审核
	 * @param adminId
	 * @param auditStatus
	 * @param reason
	 * @param selectedIDs
	 * @return
	 */
	@RequestMapping("/passAudit")
    public Object passAudit(@LoginAdmin Integer adminId,
    		Integer auditStatus,
    		String reason,
    		String selectedIDs ) {
		if(adminId == null){
			return ResponseUtil.fail401();
		}
    	if(selectedIDs == null || auditStatus == null) {
    		return ResponseUtil.fail402();
    	}
    	String[] ids = selectedIDs.split(",");
    	 Map<String, Object> data = userService.passAudit(adminId ,auditStatus, reason, ids);

		return ResponseUtil.ok(data);
	}

	/**
	 * 单个认证审核
	 * @return
	 */
	@RequestMapping(value = "auditUser")
	public Object auditUser(@LoginAdmin Integer adminId, @RequestBody AlumniUser user) {
		if(adminId == null){
			return ResponseUtil.fail401();
		}
    	if(user == null) {
    		return ResponseUtil.fail402();
    	}
		logger.debug(user);
		Map<String, Object> data = userService.auditUser(adminId ,user);
        return ResponseUtil.ok(data);
	}

	/**
	 * 用户详情
	 * @param id
	 * @return
	 */
	@RequestMapping("/detail")
	public Object detail(@RequestParam(value = "id")Integer id) {
		if(id == null){
			return ResponseUtil.fail402();
		}
		Map user = userService.findAlumniUserByUserId(id);
		Map<String, Object> data = new HashMap<>();
		data.put("items", user);

		return ResponseUtil.ok(data);
	}

	/**
	 * 查询我关注的用户
	 * @param page
	 * @return
	 */
	@RequestMapping("/findUserConcerned")
	public Object findUserConcerned(@RequestBody AlumniUser user,
			@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "limit", defaultValue = "10") Integer size) {
		if(user == null){
			return ResponseUtil.fail402();
		}
		Map<String, Object> data = userService.selectUserConcernedByUserId(user.getId(), page, size);
		return ResponseUtil.ok(data);
	}

	/**
	 * 查询我加入的圈子
	 * @param id
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping("findMyOrganize")
	public Object findMyOrganize(@RequestParam Integer id,
			@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "limit", defaultValue = "10") Integer size) {
		if(id == null) {
			return ResponseUtil.fail402();
		}
		Map<String, Object> data = userService.findMyOrganize(id, page, size);
		return ResponseUtil.ok(data);
	}

}
